//
//  PlistManager.m
//  OneCard
//
//  Created by sally on 16/2/24.
//  Copyright © 2016年 koalac. All rights reserved.
//

#import "PlistManager.h"
#import <FCFileManager.h>

static PlistManager *_instance = nil;

@implementation PlistManager

+ (PlistManager *)shareInstance {
  if (!_instance) {
    @synchronized(self) {
      if (!_instance) {
        _instance = [[self alloc] init];
      }
    }
  }
  return _instance;
}

- (NSString *)plistPathWithIdentify:(NSString *)identify {
  NSString *path = [NSString stringWithFormat:@"%@.plist", identify];
  return [FCFileManager pathForTemporaryDirectoryWithPath:path];
}

- (BOOL)isPlistExists:(NSString *)identify {
  NSString *path = [self plistPathWithIdentify:identify];
  return [FCFileManager existsItemAtPath:path];
}

- (NSMutableDictionary *)getSavedInPlist:(NSString *)identify {
  NSMutableDictionary *mdict = nil;
  if ([self isPlistExists:identify]) {
    mdict = [[NSMutableDictionary alloc] initWithContentsOfFile:[self plistPathWithIdentify:identify]];
  }
  if (!mdict) {
    mdict = [NSMutableDictionary dictionary];
  }
  return mdict;
}

- (NSMutableArray *)getArraySavedInPlist:(NSString *)identify {
  NSMutableArray *arr = nil;
  if ([self isPlistExists:identify]) {
    arr = [[NSMutableArray alloc] initWithContentsOfFile:[self plistPathWithIdentify:identify]];
  }
  if (!arr) {
    arr = [NSMutableArray array];
  }
  return arr;
}

- (BOOL)saveToPlist:(NSString *)identify andDictionary:(NSMutableDictionary *)dict {
  if (dict) {
    NSString *plistFile = [self plistPathWithIdentify:identify];
    NSLog(@"plistFile------:%@", plistFile);
    return [dict writeToFile:plistFile atomically:YES];
  }
  return NO;
}

- (BOOL)saveToPlist:(NSString *)identify andArray:(NSMutableArray *)arr {
  if (arr) {
    NSString *plistFile = [self plistPathWithIdentify:identify];
    return [arr writeToFile:plistFile atomically:YES];
  }
  return NO;
}

- (void)removePlistFileWithIdentify:(NSString *)identify {
  if ([self isPlistExists:identify]) {
    NSString *plistFile = [self plistPathWithIdentify:identify];

    [FCFileManager removeItemAtPath:plistFile];
  }
}

@end
